home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Tools / Languages / Icon 8.1 / mem / MPW Helpers / Procedures / colmize.icn next >
Encoding:
Text File  |  1992-11-08  |  2.8 KB  |  85 lines  |  [TEXT/MPS ]

  1. ############################################################################
  2. #
  3. #       Name:   colmize.icn
  4. #
  5. #       Title:  Arrange data into columns
  6. #
  7. #       Author: Robert J. Alexander
  8. #
  9. #       Date:   April 16, 1992
  10. #
  11. ############################################################################
  12. #
  13. #  colmize() -- Arrange data into columns.
  14. #
  15. #  Procedure to arrange a number of data items into multiple columns.
  16. #  Items are arranged in column-wise order, that is, the sequence runs
  17. #  down the first column, then down the second, etc.
  18. #
  19. #  This procedure goes to great lengths to print the items in as few
  20. #  vertical lines as possible.
  21. #
  22.  
  23. procedure colmize(entries,maxcols,space,minwidth,rowwise,maxspace)
  24.    local mean,cols,lines,width,i,x,wid,extra,t,j
  25.    #
  26.    #  Process arguments -- provide defaults.
  27.    #
  28.    # entries: a list of items to be columnized
  29.    /maxcols := 80                        # max width of output lines
  30.    /space := 2                           # min nbr of spaces between columns
  31.    /minwidth := 0                        # min column width
  32.    # rowwise: if nonnull, entries are listed in rowwise order rather than
  33.    #   columnwise
  34.    # maxspace: if nonnull, space is added between columns to fill the
  35.    #   width specified by maxcols, but not more than maxspace spaces.
  36.    #
  37.    #  Starting with a trial number-of-columns that is guaranteed
  38.    #  to be too wide, successively reduce the number until the
  39.    #  items can be packed into the allotted width.
  40.    #
  41.    mean := 0
  42.    every mean +:= *!entries
  43.    mean := mean / (0 ~= *entries) | 1
  44.    every cols := (maxcols + space) * 2 / (mean + space) to 1 by -1 do {
  45.       lines := (*entries + cols - 1) / cols
  46.       width := list(cols,minwidth)
  47.       i := 0
  48.       if /rowwise then {                  # if column-wise
  49.          every x := !entries do {
  50.             width[i / lines + 1] <:= *x + space
  51.             i +:= 1
  52.             }
  53.          }
  54.       else {                              # else row-wise
  55.          every x := !entries do {
  56.             width[i % cols + 1] <:= *x + space
  57.             i +:= 1
  58.             }
  59.          }
  60.       wid := 0
  61.       every x := !width do wid +:= x
  62.       if wid <= maxcols + space then break
  63.       }
  64.    #
  65.    #  Now output the data in columns.
  66.    #
  67.    extra := (\maxspace >= (maxcols - wid) / (0 < cols - 1)) | 0
  68.    if /rowwise then {            # if column-wise
  69.       every i := 1 to lines do {
  70.          t := ""
  71.          every j := 0 to cols - 1 do
  72.                t ||:= left(entries[i + j * lines],width[j + 1] + extra)
  73.          suspend trim(t)
  74.          }
  75.       }
  76.    else {                                # else row-wise
  77.       every i := 0 to lines - 1 do {
  78.          t := ""
  79.          every j := 1 to cols do
  80.                t ||:= left(entries[j + i * cols],width[j] + extra)
  81.          suspend trim(t)
  82.          }
  83.       }
  84. end
  85.